angular.module('ionicApp.controllers', [])

//登录
.controller('SignInCtrl', function($scope, $http, $state, $timeout, $ionicPopup) {
	$scope.signIn = function(user) {
//		$http.get(API_URl + 'UsersServlet?methodType=staffLogin&phoneNumber='+ user.phoneNumber+'&password='+ user.password).success(function(data){
//		$http.post(API_URl + 'UsersServlet?methodType=staffLogin',user,{headers: {'Content-Type': 'application/json'} }).success(function(data){
		//myobject = {'one':'1','two':'2','three':'3'}

		$http({
			method: 'POST',
			url: API_URl + 'UsersServlet?methodType=staffLogin',
			data: Object.toparams(user),
			headers: {'Content-Type': 'application/x-www-form-urlencoded'}
		}).success(function(data){
			console.log(user)
		if(data.status.code == 0){
			var successPopup = $ionicPopup.show({
				title: '提示',
				content:'登录成功'
			});
			$timeout(function() {
				successPopup.close();
				// 社区代表6/居委会用户7/物业用户8
				if(data.data.user_type == 7) {
					storage.setItem('introId', data.data.introId);
					$state.go("app.committee");
				}else if(data.data.user_type == 8){
					storage.setItem('introId', data.data.introId);
					$state.go("app.property");
				}else{
					console.log(data)
					storage.setItem('businessName', data.data.business[0].businessName);
					storage.setItem('businessId', data.data.business[0].businessId);
					$state.go('app.home');
				}
			}, 1500);

			storage.setItem('phoneNumber',user.phoneNumber);
			storage.setItem('id',data.data.id);
			storage.setItem('name',data.data.name);
			storage.setItem('nickname',data.data.nickname);
			storage.setItem('user_type',data.data.user_type);
			storage.setItem('imgUrl',data.data.imgUrl);
			storage.setItem('imgMiniUrl',data.data.imgMiniUrl);
			storage.setItem('bio',data.data.bio);
			storage.setItem('email',data.data.email);
			storage.setItem('birthdate',data.data.birthdate);
			storage.setItem('created_at',data.data.created_at);
			storage.setItem('gender',data.data.gender);
			storage.setItem('homepage_switch',data.data.homepage_switch);
			storage.setItem('buzz_switch',data.data.buzz_switch);
			storage.setItem('introId',data.data.introId);

		}else{
			var chanelPopup = $ionicPopup.show({
				title:'提示',
				content: data.status.msg
			});
			$timeout(function() {
				chanelPopup.close();
			}, 1500);
		}
		}).error(function(){
			var errorPopup = $ionicPopup.show({
				title:'提示',
				content: '网络错误'
			});
			$timeout(function() {
				errorPopup.close();
			}, 1500);
		});
	};
})
//menu
.controller('MenuCtrl', function($scope, $state, $http, $ionicActionSheet) {
	//$http.get(API_URl + "UsersServlet?methodType=getUser&userId=" + storage.id).success(function(data){}
	var img_url = '';
	storage.imgUrl?img_url = 'images/icon/person_default.jpg':img_url = imgURL + storage.imgUrl;
	$scope.user_info = {imgUrl:img_url,name:storage.name,phoneNumber:storage.phoneNumber, businessName:storage.businessName};

	$scope.user_type = storage.user_type;
	$scope.getHref = function(){
		if(storage.user_type == 7){
			return getHref = '#/app/committee'
		}else if(storage.user_type == 8){
			return getHref = '#/app/property'
		}else{
			return getHref = '#/app/home'
		}
	};

	$scope.logout = function () {

		$ionicActionSheet.show({
			destructiveText: '注销',
			cancelText: '取消',
			cancel: function () {
				console.log('CANCELLED');
			},
			buttonClicked: function (index) {
				console.log('BUTTON CLICKED', index);
				return true;

			},
			destructiveButtonClicked: function () {
				storage.clear();
				$state.go('signin')
			}
		});
	};
})
//首页
.controller('HomeCtrl', function($scope,$ionicNavBarDelegate) {

})

//商户管理tab
.controller('BusinessTabsCtrl',function ($scope) {
	$scope.business_tabs = [{
		title: '所有商户',
		url: 'all_business.html'
	}, {
		title: '我的商户',
		url: 'my_business.html'
	}];

	$scope.currentTab = 'all_business.html';

	$scope.onClickTab = function (tab) {
		$scope.currentTab = tab.url;
	};

	$scope.isActiveTab = function(tabUrl) {
		return tabUrl == $scope.currentTab;
	}
})

//商户管理
.controller('BusinessManageCtrl', function($scope, $http, $ionicLoading) {
//longitude=116.429&latitude=40.0407&
	//所有商户
	$ionicLoading.show({
		template: 'Loading...'
	});
	$http.get(searchURL + '/business/detailCount.ac?size=10&range=5&sorttype=0&statisticsFields=business_category_id').success(function(data){
		$scope.all_business = data.data[0].result;
		$ionicLoading.hide();
	}).error(function(status){
		alert("网络错误");
		$ionicLoading.hide();
	});
	//我的商户
	var myId = storage.getItem('id');
	$http.get(searchURL + '/business/detailCount.ac?size=10&range=5&sorttype=1&statisticsFields=business_category_id&factor=creator_id must '+myId).success(function(data){
		$scope.my_business = data.data[0].result;
		$ionicLoading.hide();
	}).error(function(status){
		alert("网络错误");
		$ionicLoading.hide();
	});

})


//商户列表
.controller('SearchListCtrl', function($scope, $http, $stateParams, $state, $timeout, $ionicPopup, $ionicLoading,$rootScope, $ionicNavBarDelegate) {

		$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
		 //assign the "from" or "to" parameter to something
		 //console.log(toParams,to,from)

		 });
	//longitude='+ mylongitude +'&latitude='+ mylatitude +'
	//longitude=116.429&latitude=40.0407
	localStorage.business_category_id=$stateParams.business_category_id;
	$ionicLoading.show({
		template: 'Loading...'
	});
	$http.get(searchURL + '/business/keywordSearch.ac?longitude='+ mylongitude +'&latitude='+ mylatitude +'&range=5&from=0&size=100&sorttype=1&factor=business_category_id must '+$stateParams.business_category_id).success(function(data){
		$scope.search_list = data.data[0].result;
		$ionicLoading.hide();
	}).error(function(status){
		alert("网络错误");
		$ionicLoading.hide();
	});

	//点击搜索商户
	$scope.searchList = function(business){
		//longitude='+ mylongitude +'&latitude='+ mylatitude +'
		//longitude=116.429&latitude=40.0407
		if(!business.keyword){
			var emptyPopup = $ionicPopup.show({
				content: '请输入关键词!'
			});
			$timeout(function() {
				emptyPopup.close();
			}, 1500);
		}else{
			$ionicLoading.show({
				template: 'Loading...'
			});
			$http.get(searchURL + '/business/keywordSearch.ac?keyword=' + business.keyword + 'longitude='+ mylongitude +'&latitude='+ mylatitude +'&range=5&from=0&size=100&sorttype=1&factor=business_category_id must '+$stateParams.business_category_id).success(function(data){
				$scope.search_list = data.data[0].result;
				$ionicLoading.hide();
			}).error(function(status){
				alert("网络错误");
				$ionicLoading.hide();
			});
		}

	};

	$scope.addMerchant = function(){
		$state.go('app.add_merchant');
	}
})


//添加商户
.controller('AddMerchantCtrl', function($scope, $http, $state){
	//获取当前位置经纬度
	function fixed_position(){
		 setTimeout(function (){
		 getLocationInfo(function(locationArray){
			 $scope.latitude = locationArray[0];//经度
		 	 $scope.longitude = locationArray[1];//纬度
			 $scope.currentAddress = locationArray[2];
		 	 document.getElementById('currentAddress').value = locationArray[2];

			 $http.get(searchURL + 'business/keywordSearch.ac?longitude=' + $scope.longitude + '&latitude=' + $scope.latitude + '&sorttype=1').success(function(data){
				 $scope.listDisplays = data.data[0].result;
			 });
		 },function(str){
			 alert(str);
		 });
		 },100);
	}
	fixed_position();

	$scope.positionCorrection = function(){
		fixed_position();
	};
		//longitude=116.429&latitude=40.0407&
	$scope.listDisplay = function(opt){
		var seek_input = opt.textValue;
		//$http.get(searchURL + 'business/keywordSearch.ac?sorttype=1&factor=name must'+ seek_input + '&longitude=' + $scope.longitude + '&latitude=' + $scope.latitude + '&range=5').success(function(data){
		$http.get(searchURL + 'business/keywordSearch.ac?sorttype=1&factor=name must '+seek_input+'&longitude=' + $scope.longitude + '&latitude=' + $scope.latitude + '&range=5').success(function(data){
			$scope.listDisplays = data.data[0].result;
		})
	};
	$scope.opt = {textValue:''};
	$scope.next = function(opt){
		if(!opt.textValue){
			localStorage.removeItem('seek_input');
		}else{
			storage.seek_input = opt.textValue;
		}
		$state.go('app.add_merchant_next',{view_title:'add'});
	};

})

//添加商户下一步
.controller('AddMerchantNextCtrl', function($scope, $http, $state, $stateParams, $compile, $timeout, $ionicPopup, $ionicLoading){
		$ionicLoading.show({
			template: 'Loading...'
		});
		if($stateParams.view_title == 'edit'){
			$scope.view_title = '编辑商户';
			$scope.shopLatitude = '';
				$http.get(API_URl + 'StaffBusinessServlet?methodType=getById&businessId='+$stateParams.business_id).success(function(data){
					$ionicLoading.hide();
					if(data.status.code == 0){
						$scope.addInfo = {seek_input: data.data.name, merchantAddress: data.data.address, merchantPhone: data.data.phone, merchantIntro: data.data.intro,shopLatitude: data.data.latitude,shopLongitude: data.data.longitude};

						if(data.data.imgUrl && data.data.imgMiniUrl){
							var editPhoto00 = document.getElementById('add-photo-00');
							var exitImg00 = $compile('<div class="glass_img" _src="'+ data.data.imgUrl +'" ' +
								'_minisrc="'+ data.data.imgMiniUrl +'" style="background-image:url('+ imgURL + data.data.imgMiniUrl +
								')" ><p class="del_btn" ng-click="removeBtn($event.target)"></p></div>')($scope);
								angular.element(editPhoto00).prepend(exitImg00);
								angular.element(editPhoto00).find('img').css('display','none');
						}

						for(var i=0 ;i<data.data.shopInImgs.length; i++){
							var editPhoto01 = document.getElementById('add-photo-01');
							var exitImg01 = $compile('<div class="glass_img" _src="'+ data.data.shopInImgs[i].imgUrl +'" ' +
								'_minisrc="'+ data.data.shopInImgs[i].imgMiniUrl +'" style="background-image:url('+ imgURL +  data.data.shopInImgs[i].imgMiniUrl +
								')" ><p class="del_btn" ng-click="removeBtn($event.target)"></p></div>')($scope);
							angular.element(editPhoto01).prepend(exitImg01);
						}

						for(var i=0 ;i<data.data.shopDishesImgs.length; i++){
							var editPhoto02 = document.getElementById('add-photo-02');
							var exitImg02 = $compile('<div class="glass_img" _src="'+ data.data.shopDishesImgs[i].imgUrl +'" ' +
								'_minisrc="'+ data.data.shopDishesImgs[i].imgMiniUrl +'" style="background-image:url('+ imgURL +  data.data.shopDishesImgs[i].imgMiniUrl +
								')" ><p class="del_btn" ng-click="removeBtn($event.target)"></p></div>')($scope);
							angular.element(editPhoto02).prepend(exitImg02);
						}

						for(var i=0; i<data.data.shopOthersImgs.length; i++){
							var editPhoto03 = document.getElementById('add-photo-03');
							var exitImg03 = $compile('<div class="glass_img" _src="'+ data.data.shopOthersImgs[i].imgUrl +'" ' +
								'_minisrc="'+ data.data.shopOthersImgs[i].imgMiniUrl +'" style="background-image:url('+ imgURL +  data.data.shopOthersImgs[i].imgMiniUrl +
								')" ><p class="del_btn" ng-click="removeBtn($event.target)"></p></div>')($scope);
							angular.element(editPhoto03).prepend(exitImg03);
						}

					}

				}).error(function(){

				});


			var addMerchants = {};
			$scope.addMerchant = function(addInfo){
				addMerchants.shopHeadImgs = '';
				addMerchants.shopHeadImgs_MINI = '';
				addMerchants.shopInImgs = '';
				addMerchants.shopInImgs_MINI = '';
				addMerchants.shopDishesImgs = '';
				addMerchants.shopDishesImgs_MINI = '';
				addMerchants.shopOthersImgs = '';
				addMerchants.shopOthersImgs_MINI = '';


				$scope.latitude = addInfo.shopLatitude;
				$scope.longitude = addInfo.shopLongitude;
				console.log($scope.latitude,$scope.longitude);


				var addPhoto00 = document.getElementById('add-photo-00').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto00.length; i++){
					if(!addMerchants.shopHeadImgs && !addMerchants.shopHeadImgs_MINI){
						addMerchants.shopHeadImgs = addPhoto00[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopHeadImgs_MINI = addPhoto00[i].getAttribute('_minisrc').replace('&','%26');
					}else{
						addMerchants.shopHeadImgs += ',' + addPhoto00[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopHeadImgs_MINI += ',' + addPhoto00[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				var addPhoto01 = document.getElementById('add-photo-01').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto01.length; i++){
					if(!addMerchants.shopInImgs && !addMerchants.shopInImgs_MINI){
						addMerchants.shopInImgs = addPhoto01[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopInImgs_MINI = addPhoto01[i].getAttribute('_minisrc').replace('&','%26');
					}else{
						addMerchants.shopInImgs += ',' + addPhoto01[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopInImgs_MINI += ',' + addPhoto01[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				var addPhoto02 = document.getElementById('add-photo-02').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto02.length; i++){
					if(!addMerchants.shopDishesImgs && !addMerchants.shopDishesImgs_MINI){
						addMerchants.shopDishesImgs = addPhoto02[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopDishesImgs_MINI = addPhoto02[i].getAttribute('_minisrc').replace('&','%26');
					}else{
						addMerchants.shopDishesImgs += ',' + addPhoto02[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopDishesImgs_MINI += ',' + addPhoto02[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				var addPhoto03 = document.getElementById('add-photo-03').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto03.length; i++){
					if(!addMerchants.shopOthersImgs && !addMerchants.shopOthersImgs_MINI){
						addMerchants.shopOthersImgs = addPhoto03[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopOthersImgs_MINI = addPhoto03[i].getAttribute('_minisrc').replace('&','%26');
					}else {
						addMerchants.shopOthersImgs += ',' + addPhoto03[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopOthersImgs_MINI += ',' + addPhoto03[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				addInfo.merchantAddress=document.getElementById('currentAddressNext').value;

//				addMerchants.shopHeadImgs = addMerchants.shopHeadImgs.replace('&','%26');
				addInfo.addData = {
					businessId: $stateParams.business_id,
					name: addInfo.seek_input,
					address: addInfo.merchantAddress,
					phone: addInfo.merchantPhone,
					intro: addInfo.merchantIntro,
					businessTypeId:storage.business_category_id,
					longitude:$scope.longitude,
					latitude:$scope.latitude,
					shopHeadImgs: addMerchants.shopHeadImgs,
					shopHeadImgs_MINI: addMerchants.shopHeadImgs_MINI,
					shopInImgs: addMerchants.shopInImgs,
					shopInImgs_MINI: addMerchants.shopInImgs_MINI,
					shopDishesImgs: addMerchants.shopDishesImgs,
					shopDishesImgs_MINI: addMerchants.shopDishesImgs_MINI,
					shopOthersImgs: addMerchants.shopOthersImgs,
					shopOthersImgs_MINI: addMerchants.shopOthersImgs_MINI
				};
				console.log(addInfo.addData);

				/*$http.get(API_URl + 'StaffBusinessServlet?methodType=update&businessId='+ $stateParams.business_id +'&name='+addInfo.seek_input+'&address='+addInfo.merchantAddress+'&phone='+addInfo.merchantPhone+
						'&intro='+addInfo.merchantIntro+'&businessTypeId='+storage.business_category_id+'&longitude='+$scope.longitude+'&latitude='+$scope.latitude+'&shopHeadImgs='+addMerchants.shopHeadImgs+
						'&shopHeadImgs_MINI='+addMerchants.shopHeadImgs_MINI+'&shopInImgs='+addMerchants.shopInImgs+'&shopInImgs_MINI='+addMerchants.shopInImgs_MINI+'&shopDishesImgs='+addMerchants.shopDishesImgs+
						'&shopDishesImgs_MINI='+addMerchants.shopDishesImgs_MINI+'&shopOthersImgs='+addMerchants.shopOthersImgs+'&shopOthersImgs_MINI='+addMerchants.shopOthersImgs_MINI
				)*/
				$http({
					method: 'POST',
					url: API_URl + 'StaffBusinessServlet?methodType=update',
					data: Object.toparams(addInfo.addData),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).success(function(data){
						addInfo.mydata = data;
						if(data.status.code == 0) {
							var successPopup = $ionicPopup.show({
								content: '编辑成功!'
							});
							$timeout(function() {
								successPopup.close();
								//$state.go('app.merchant_view',{'id':$stateParams.business_id});
							}, 1500);
						}else{
							var successPopup = $ionicPopup.show({
								content: '请完善商户信息!'
							});
							$timeout(function() {
								successPopup.close();
							}, 1500);
						}
					}).error(function(data, status, headers, config){
						alert("error");
					});
			};
		}else if($stateParams.view_title == 'add') {
			$ionicLoading.hide();
			$scope.view_title = '添加商户';
			$scope.addInfo = {seek_input: storage.seek_input};
			fixed_position();

			var addMerchants = {};

			$scope.addMerchant = function(addInfo){
				addMerchants.shopHeadImgs = '';
				addMerchants.shopHeadImgs_MINI = '';
				addMerchants.shopInImgs = '';
				addMerchants.shopInImgs_MINI = '';
				addMerchants.shopDishesImgs = '';
				addMerchants.shopDishesImgs_MINI = '';
				addMerchants.shopOthersImgs = '';
				addMerchants.shopOthersImgs_MINI = '';

				var addPhoto00 = document.getElementById('add-photo-00').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto00.length; i++){
					if(!addMerchants.shopHeadImgs && !addMerchants.shopHeadImgs_MINI){
						addMerchants.shopHeadImgs = addPhoto00[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopHeadImgs_MINI = addPhoto00[i].getAttribute('_minisrc').replace('&','%26');
					}else{
						addMerchants.shopHeadImgs += ',' + addPhoto00[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopHeadImgs_MINI += ',' + addPhoto00[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				var addPhoto01 = document.getElementById('add-photo-01').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto01.length; i++){
					if(!addMerchants.shopInImgs && !addMerchants.shopInImgs_MINI){
						addMerchants.shopInImgs = addPhoto01[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopInImgs_MINI = addPhoto01[i].getAttribute('_minisrc').replace('&','%26');
					}else{
						addMerchants.shopInImgs += ',' + addPhoto01[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopInImgs_MINI += ',' + addPhoto01[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				var addPhoto02 = document.getElementById('add-photo-02').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto02.length; i++){
					if(!addMerchants.shopDishesImgs && !addMerchants.shopDishesImgs_MINI){
						addMerchants.shopDishesImgs = addPhoto02[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopDishesImgs_MINI = addPhoto02[i].getAttribute('_minisrc').replace('&','%26');
					}else{
						addMerchants.shopDishesImgs += ',' + addPhoto02[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopDishesImgs_MINI += ',' + addPhoto02[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				var addPhoto03 = document.getElementById('add-photo-03').getElementsByTagName('div');
				for(var i=0 ;i<addPhoto03.length; i++){
					if(!addMerchants.shopOthersImgs && !addMerchants.shopOthersImgs_MINI){
						addMerchants.shopOthersImgs = addPhoto03[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopOthersImgs_MINI = addPhoto03[i].getAttribute('_minisrc').replace('&','%26');
					}else {
						addMerchants.shopOthersImgs += ',' + addPhoto03[i].getAttribute('_src').replace('&','%26');
						addMerchants.shopOthersImgs_MINI += ',' + addPhoto03[i].getAttribute('_minisrc').replace('&','%26');
					}
				}

				addInfo.merchantAddress=document.getElementById('currentAddressNext').value;
				addInfo.addData = {
					userId: storage.id,
					name: addInfo.seek_input,
					address: addInfo.merchantAddress,
					phone: addInfo.merchantPhone,
					intro: addInfo.merchantIntro,
					businessTypeId:storage.business_category_id,
					longitude:$scope.longitude,
					latitude:$scope.latitude,
					shopHeadImgs: addMerchants.shopHeadImgs,
					shopHeadImgs_MINI: addMerchants.shopHeadImgs_MINI,
					shopInImgs: addMerchants.shopInImgs,
					shopInImgs_MINI: addMerchants.shopInImgs_MINI,
					shopDishesImgs: addMerchants.shopDishesImgs,
					shopDishesImgs_MINI: addMerchants.shopDishesImgs_MINI,
					shopOthersImgs: addMerchants.shopOthersImgs,
					shopOthersImgs_MINI: addMerchants.shopOthersImgs_MINI
				};
				console.log(addInfo.addData);

				/*$http.get(API_URl + 'StaffBusinessServlet?methodType=add&userId='+ storage.id +'&name='+addInfo.seek_input+'&address='+addInfo.merchantAddress+'&phone='+addInfo.merchantPhone+
						'&intro='+addInfo.merchantIntro+'&businessTypeId='+storage.business_category_id+'&longitude='+$scope.longitude+'&latitude='+$scope.latitude+'&shopHeadImgs='+addMerchants.shopHeadImgs+
						'&shopHeadImgs_MINI='+addMerchants.shopHeadImgs_MINI+'&shopInImgs='+addMerchants.shopInImgs+'&shopInImgs_MINI='+addMerchants.shopInImgs_MINI+'&shopDishesImgs='+addMerchants.shopDishesImgs+
						'&shopDishesImgs_MINI='+addMerchants.shopDishesImgs_MINI+'&shopOthersImgs='+addMerchants.shopOthersImgs+'&shopOthersImgs_MINI='+addMerchants.shopOthersImgs_MINI
				)*/
				$http({
					method: 'POST',
					url: API_URl + 'StaffBusinessServlet?methodType=add',
					data: Object.toparams(addInfo.addData),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).success(function(data){
						addInfo.mydata = data;
						if(data.status.code == 0) {
							var successPopup = $ionicPopup.show({
								content: '添加成功!'
							});
							$timeout(function() {
								successPopup.close();
								$state.go('app.add_merchant_success',{'id':data.data.businessId});
							}, 1500);
						}else{
							var emptyPopup = $ionicPopup.show({
								content: '请完善商户信息!'
							});
							$timeout(function() {
								emptyPopup.close();
							}, 1500);
						}
					}).error(function(data, status, headers, config){
						alert("error");
					});
			};
		}



	function fixed_position(){
		setTimeout(function (){
			getLocationInfo(function(locationArray){
				$scope.latitude = locationArray[0];//经度
				$scope.longitude = locationArray[1];//纬度
				document.getElementById('currentAddressNext').value = locationArray[2];
			},function(str){
				alert(str);
			});
		},100);
	}

	$scope.getAddress = function(){
		fixed_position();
	};

	$scope.delMerchantName = function(addInfo){
		addInfo.seek_input = '';
		localStorage.removeItem('seek_input');
	};

//	上传门头照
	function upImage(obj,imglength,callback){
		//showDiv();
		//var imgUp = 'http://218.247.15.102/imgup/fileUpload/uploadFileAction.action';
		getPhotoFromCamera(function (imageURL){
			uploadImage(IMG_UPLOAD_URL,[imageURL],'CommunityInterface',300,300,
				function (webImgUrl){
					/*obj.append('<span class="glass_img" _src="'+ webImgUrl.success_image_url[0].imgPath +'" ' +
						'_minisrc="'+ webImgUrl.success_image_url[0].miniImgPath +'" style="background-image:url('+ imageURL +
						')" ></span><p class="del_btn" ng-click="removeBtn()"></p>');*/

					var upImg = $compile('<div class="glass_img" _src="'+ webImgUrl.success_image_url[0].imgPath +'" ' +
						'_minisrc="'+ webImgUrl.success_image_url[0].miniImgPath +'" style="background-image:url('+ imageURL +
						')" ><p class="del_btn" ng-click="removeBtn($event.target)"></p></div>')($scope);


					obj.append(upImg);


					if(callback == 'hide'){
						obj.find('img').css('display','none');
					}
				},function (){
					alert('上传失败');
				})

		},function (){
			alert('上传失败');
		});
	}

	$scope.addPhoto00 = function(target){
		var _this = angular.element(target).parent();
		upImage(_this, 1, 'hide');
	};

	$scope.addPhoto01 = function(target){
		var _this = angular.element(target).parent();
		upImage(_this, 6);
	};

	$scope.addPhoto02 = function(target){
		var _this = angular.element(target).parent();
		upImage(_this, 6);
	};

	$scope.addPhoto03 = function(target){
		var _this = angular.element(target).parent();
		upImage(_this, 6);
	};



	$scope.goback = function(){
		history.back();
	};

	$scope.removeBtn = function(target){
		angular.element(target).parent().remove();
		var addphotoLen = document.getElementById('add-photo-00');
		if(angular.element(addphotoLen).find('div').length < 1 ){
			angular.element(addphotoLen).find('img').css('display','block');
		}
	};

		/*var addMerchants = {};

		$scope.addMerchant = function(addInfo){
			addMerchants.shopHeadImgs = '';
			addMerchants.shopHeadImgs_MINI = '';
			addMerchants.shopInImgs = '';
			addMerchants.shopInImgs_MINI = '';
			addMerchants.shopDishesImgs = '';
			addMerchants.shopDishesImgs_MINI = '';
			addMerchants.shopOthersImgs = '';
			addMerchants.shopOthersImgs_MINI = '';

			var addPhoto00 = document.getElementById('add-photo-00').getElementsByTagName('div');
			for(var i=0 ;i<addPhoto00.length; i++){
				if(!addMerchants.shopHeadImgs && !addMerchants.shopHeadImgs_MINI){
					addMerchants.shopHeadImgs = addPhoto00[i].getAttribute('_src');
					addMerchants.shopHeadImgs_MINI = addPhoto00[i].getAttribute('_minisrc');
				}else{
					addMerchants.shopHeadImgs += ',' + addPhoto00[i].getAttribute('_src');
					addMerchants.shopHeadImgs_MINI += ',' + addPhoto00[i].getAttribute('_minisrc');
				}
			}

			var addPhoto01 = document.getElementById('add-photo-01').getElementsByTagName('div');
			for(var i=0 ;i<addPhoto01.length; i++){
				if(!addMerchants.shopInImgs && !addMerchants.shopInImgs_MINI){
					addMerchants.shopInImgs = addPhoto01[i].getAttribute('_src');
					addMerchants.shopInImgs_MINI = addPhoto01[i].getAttribute('_minisrc');
				}else{
					addMerchants.shopInImgs += ',' + addPhoto01[i].getAttribute('_src');
					addMerchants.shopInImgs_MINI += ',' + addPhoto01[i].getAttribute('_minisrc');
				}
			}

			var addPhoto02 = document.getElementById('add-photo-02').getElementsByTagName('div');
			for(var i=0 ;i<addPhoto02.length; i++){
				if(!addMerchants.shopDishesImgs && !addMerchants.shopDishesImgs_MINI){
					addMerchants.shopDishesImgs = addPhoto02[i].getAttribute('_src');
					addMerchants.shopDishesImgs_MINI = addPhoto02[i].getAttribute('_minisrc');
				}else{
					addMerchants.shopDishesImgs += ',' + addPhoto02[i].getAttribute('_src');
					addMerchants.shopDishesImgs_MINI += ',' + addPhoto02[i].getAttribute('_minisrc');
				}
			}

			var addPhoto03 = document.getElementById('add-photo-03').getElementsByTagName('div');
			for(var i=0 ;i<addPhoto03.length; i++){
				if(!addMerchants.shopOthersImgs && !addMerchants.shopOthersImgs_MINI){
					addMerchants.shopOthersImgs = addPhoto03[i].getAttribute('_src');
					addMerchants.shopOthersImgs_MINI = addPhoto03[i].getAttribute('_minisrc');
				}else {
					addMerchants.shopOthersImgs += ',' + addPhoto03[i].getAttribute('_src');
					addMerchants.shopOthersImgs_MINI += ',' + addPhoto03[i].getAttribute('_minisrc');
				}
			}

			addInfo.merchantAddress=document.getElementById('currentAddressNext').value;
			addInfo.addData = {
				userId: storage.id,
				name: addInfo.seek_input,
				address: addInfo.merchantAddress,
				phone: addInfo.merchantPhone,
				intro: addInfo.merchantIntro,
				businessTypeId:storage.business_category_id,
				longitude:$scope.longitude,
				latitude:$scope.latitude,
				shopHeadImgs: addMerchants.shopHeadImgs,
				shopHeadImgs_MINI: addMerchants.shopHeadImgs_MINI,
				shopInImgs: addMerchants.shopInImgs,
				shopInImgs_MINI: addMerchants.shopInImgs_MINI,
				shopDishesImgs: addMerchants.shopDishesImgs,
				shopDishesImgs_MINI: addMerchants.shopDishesImgs_MINI,
				shopOthersImgs: addMerchants.shopOthersImgs,
				shopOthersImgs_MINI: addMerchants.shopOthersImgs_MINI
			};
			console.log(addInfo.addData);

			$http.get(API_URl + 'StaffBusinessServlet?methodType=add&userId='+ storage.id +'&name='+addInfo.seek_input+'&address='+addInfo.merchantAddress+'&phone='+addInfo.merchantPhone+
					'&intro='+addInfo.merchantIntro+'&businessTypeId='+storage.business_category_id+'&longitude='+$scope.longitude+'&latitude='+$scope.latitude+'&shopHeadImgs='+addMerchants.shopHeadImgs+
					'&shopHeadImgs_MINI='+addMerchants.shopHeadImgs_MINI+'&shopInImgs='+addMerchants.shopInImgs+'&shopInImgs_MINI='+addMerchants.shopInImgs_MINI+'&shopDishesImgs='+addMerchants.shopDishesImgs+
					'&shopDishesImgs_MINI='+addMerchants.shopDishesImgs_MINI+'&shopOthersImgs='+addMerchants.shopOthersImgs+'&shopOthersImgs_MINI='+addMerchants.shopOthersImgs_MINI
			).success(function(data){
				addInfo.mydata = data;
				if(data.status.code == 0) {
					//$state.go('app.add_merchant_success',{'id':data.data.businessId});
				}else{
					var successPopup = $ionicPopup.show({
						content: '请完善商户信息!'
					});
					$timeout(function() {
						successPopup.close();
					}, 1500);
				}
			}).error(function(data, status, headers, config){
					alert("error");
			});
		};*/



	/*****/

})

//添加商户成功
.controller('AddMerchantSuccessCtrl',function($scope, $state, $stateParams){
	$scope.addSuccess = function(){
		$state.go('app.merchant_view',{id:$stateParams.id});
	}

})


//商户详情
.controller('MerchantViewCtrl', function($scope, $http, $state, $stateParams, $ionicLoading,$rootScope,$window,$location, $ionicNavBarDelegate){


		/*$scope.goBack = function() {
//			$ionicNavBarDelegate.back();
			$state.go('app.search_list')
		};
*/
		//var back_btn = document.getElementsByTagName('ion-nav-back-button')[0];
		//back_btn.onclick=function(){
			//$location.url('/app/business_manage')
//			$window.history.go(-2);
			/*var backBackView = $ionicViewService.getView(-2);
			backBackView && backBackView.go();*/
		//};
		//console.log($rootScope.$viewHistory.backView.stateName)

		/*$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
			//assign the "from" or "to" parameter to something
//			console.log(toParams,to,from)
			if(to.name=='app.merchant_view' && from.name=='app.add_merchant_success'){
				back_btn.onclick=function(){
					//$state.go('app.search_list',{business_category_id:storage.merchants_type_id})
					//$location.url('/app/business_manage')
					$ionicNavBarDelegate.back(-2);
				};
			}
		});*/

	$ionicLoading.show({
		template: 'Loading...'
	});

		/*<ion-nav-buttons side="left">
			<button class="button" ng-click="doSomething()">
			I'm a button on the left of the navbar!
			</button>
		</ion-nav-buttons>*/

	$http.get(API_URl + "StaffBusinessServlet?methodType=getById&businessId="+$stateParams.id).success(function(data){
		$ionicLoading.hide();
		if(data.status.code == 0){
			var merchant_data;
			var objData = data.data;
			if(objData.imgUrl == '' || objData.imgUrl == null){
				objData.imgUrl = 'images/icon/merchant_default_big.png';
			}else{
				objData.imgUrl = imgURL + objData.imgUrl;
			}
//			console.log(objData.imgUrl);
			merchant_data = objData;
			$scope.merchant = merchant_data;

			$scope.bool = false;
			if(data.data.creator_id == storage.id){
				$scope.bool = true
			}else{
				$scope.bool = false;
			}

			$scope.updateMerchant = function(){
				$state.go('app.add_merchant_next',{business_id:$stateParams.id,view_title:'edit'});
			}

		}

	}).error(function(status){
			alert("网络错误");
			$ionicLoading.hide();
	});

	$scope.to_add_workman = function(){
		$state.go('app.add_workman',{business_id:$stateParams.id});
	}

})

//添加工作人员
.controller('AddWorkmanCtrl',function($scope, $http, $stateParams, $timeout, $ionicPopup){
	$scope.gender = [
		{id:'0',infoGender:'男'},
		{id:'1',infoGender:'女'}
	]
	$scope.selectedGender = $scope.gender[0];

	$http.get(API_URl + 'CodePositionServlet?methodType=getAll').success(function(data){
		$scope.duty = data.data;
		$scope.selectedName = $scope.duty[0];

	})

		function addWorkman(workmanInfo){
//			alert(workmanInfo.type);

			/*if (workmanInfo.btnType == 0) {
				alert(workmanInfo.btnType);
			}else{
				alert(workmanInfo.btnType);
			}*/

			$http.get(API_URl + 'BusinessEmployeeServlet?methodType=add&businessId='+$stateParams.business_id+'&phone='+workmanInfo.phoneNumber+'&positionId='+$scope.selectedName.id+'&name='+workmanInfo.name+'&sex='+$scope.selectedGender.infoGender+'&description=暂无简介').success(function(data){
				if(data.status.code == 0){
					if (workmanInfo.btnType == 0) {
						var successPopup = $ionicPopup.show({
							content: '提交成功!'
						});
						$timeout(function () {
							successPopup.close();
							history.back();
						}, 1000);
					}else{
						var successPopup = $ionicPopup.show({
							content: '提交成功!'
						});
						$timeout(function () {
							successPopup.close();
						}, 1000);
					}
				}else if(data.status.code == 1){
					var successPopup = $ionicPopup.show({
						content: '请重新提交!'
					});
					$timeout(function() {
						successPopup.close();
					}, 1000);
				}else if(data.status.code == 2){
					var successPopup = $ionicPopup.show({
						content: '手机号码错误!'
					});
					$timeout(function() {
						successPopup.close();
					}, 1000);
				}else if(data.status.code == 3){
					var successPopup = $ionicPopup.show({
						content: '手机号码已经被注册!'
					});
					$timeout(function() {
						successPopup.close();
					}, 1000);
				}
			});
		}

	$scope.workmanInfo=[{type:'0'},{type:'1'}];
	$scope.addWorkman = function(workmanInfo){
		workmanInfo.btnType = workmanInfo[0].type;
		addWorkman(workmanInfo);
	};
	$scope.addWorkmanGoOn = function(workmanInfo){
		workmanInfo.btnType = workmanInfo[1].type;
		addWorkman(workmanInfo);
	};
	$scope.goback = function(){
		history.back();
	};

		//console.log($scope.selectedName.id)
})

//优惠列表
.controller('PreferentialListCtrl', function($scope, $http, $ionicLoading) {
	$ionicLoading.show({
		template: 'Loading...'
	});
	$http.get(API_URl + 'PreferentialServlet?methodType=getData&auditUserId='+storage.id+'&auditStatus=0&pageNow=1&pageSize=25').success(function(data){
			var audits_data = [];
			for(var i=0;i<data.data.length;i++){
				var objData = data.data[i];
				if(objData.imgMiniUrl == '' || objData.imgMiniUrl == null){
					objData.imgMiniUrl = 'images/icon/merchant_default_small.png';
				}else{
					objData.imgMiniUrl = imgURL + objData.imgMiniUrl;
				}
				audits_data.push(objData);
			}
			$scope.audits_wait = audits_data;
			$ionicLoading.hide();
	}
	).error(function(status){
			alert("网络错误");
			$ionicLoading.hide();
	});
	$http.get(API_URl + 'PreferentialServlet?methodType=getData&auditUserId='+storage.id+'&auditStatus=2&pageNow=1&pageSize=25').success(function(data){
			var audits_data = [];
			for(var i=0;i<data.data.length;i++){
				var objData = data.data[i];
				if(objData.imgMiniUrl == '' || objData.imgMiniUrl == null){
					objData.imgMiniUrl = 'images/icon/merchant_default_small.png';
				}else{
					objData.imgMiniUrl = imgURL + objData.imgMiniUrl;
				}
				audits_data.push(objData);
			}
			$scope.audits_agree = audits_data;
		}
	).error(function(status){
			alert("网络错误");
			$ionicLoading.hide();
		});

	$http.get(API_URl + 'PreferentialServlet?methodType=getData&auditUserId='+storage.id+'&auditStatus=1&pageNow=1&pageSize=25').success(function(data){
			var audits_data = [];
			for(var i=0;i<data.data.length;i++){
				var objData = data.data[i];
				if(objData.imgMiniUrl == '' || objData.imgMiniUrl == null){
					objData.imgMiniUrl = 'images/icon/merchant_default_small.png';
				}else{
					objData.imgMiniUrl = imgURL + objData.imgMiniUrl;
				}
				audits_data.push(objData);
			}
			$scope.audits_reject = audits_data;
		}
	).error(function(status){
			alert("网络错误");
			$ionicLoading.hide();
	});
})


//待审核优惠详情
.controller('PreferentialAuditViewCtrl', function($scope, $http, $stateParams, $ionicLoading) {
	$ionicLoading.show({
		template: 'Loading...'
	});
	$http.get(API_URl + 'PreferentialServlet?methodType=getById&preferentialId=' + $stateParams.id).success(function(data){

		var objData = data.data;

		if(objData.imgUrl == '' || objData.imgUrl == null){
			objData.imgUrl = 'images/icon/merchant_default_big.png';
		}else{
			objData.imgUrl = imgURL + objData.imgUrl;
		}
		if(objData.businessImgMiniUrl == '' || objData.businessImgMiniUrl == null){
			objData.businessImgMiniUrl = 'images/icon/merchant_default_small.png';
		}else{
			objData.businessImgMiniUrl = imgURL + objData.businessImgMiniUrl;
		}
		objData.startTime = new Date(objData.startTime).format('MM-dd hh:mm');
		objData.endTime = new Date(objData.endTime).format('MM-dd hh:mm');
		$scope.preferential_view = objData;

		$ionicLoading.hide();
	});

	$scope.reject = function(){
		$ionicLoading.show({
			template: 'Loading...'
		});
		$http.get(API_URl + "PreferentialServlet?methodType=audio&preferentialId="+$stateParams.id+'&auditStatus='+2).success(function(data){
			if(data.status.code == 0){
				alert('已拒绝')
			}else{
				alert(data.status.msg)
			}
			$ionicLoading.hide();
		})
	};
	$scope.agree = function(){
		$ionicLoading.show({
			template: 'Loading...'
		});
		$http.get(API_URl + "PreferentialServlet?methodType=audio&preferentialId="+$stateParams.id+'&auditStatus='+1).success(function(data){
			if(data.status.code == 0){
				$ionicLoading.hide();
				alert('已同意')
			}else{
				$ionicLoading.hide();
				alert(data.status.msg)
			}

		})
	}

})

//优惠详情
.controller('PreferentialViewCtrl', function($scope, $http, $stateParams, $ionicLoading) {
	$ionicLoading.show({
		template: 'Loading...'
	});
	$http.get(API_URl + 'PreferentialServlet?methodType=getById&preferentialId=' + $stateParams.id).success(function(data){
		var objData = data.data;

		if(objData.imgUrl == '' || objData.imgUrl == null){
			objData.imgUrl = 'images/icon/merchant_default_big.png';
		}else{
			objData.imgUrl = imgURL + objData.imgUrl;
		}
		if(objData.businessImgMiniUrl == '' || objData.businessImgMiniUrl == null){
			objData.businessImgMiniUrl = 'images/icon/merchant_default_small.png';
		}else{
			objData.businessImgMiniUrl = imgURL + objData.businessImgMiniUrl;
		}

		if(objData.auditStatus == 1){
			objData.auditStatus = '未通过';
		}else if(objData.auditStatus = 2){
			objData.auditStatus = '已通过';
		}

		objData.startTime = new Date(objData.startTime).format('MM-dd hh:mm');
		objData.endTime = new Date(objData.endTime).format('MM-dd hh:mm');

		$scope.preferential_view = objData;
		$ionicLoading.hide();
	});

})

//优惠列表tab
	.controller('PreferentialTabsCtrl', function ($scope) {
		$scope.preferential_tabs = [{
			title: '待审核',
			url: 'audits_wait.html'
		}, {
			title: '已同意',
			url: 'audits_agree.html'
		}, {
			title: '已拒绝',
			url: 'audits_reject.html'
		}];

		$scope.currentTab = 'audits_wait.html';

		$scope.onClickTab = function (tab) {
			$scope.currentTab = tab.url;
		};

		$scope.isActiveTab = function(tabUrl) {
			return tabUrl == $scope.currentTab;
		}
	})

//个人设置
.controller('MySetCtrl',function($scope, $http, $timeout, $ionicPopup){
	$http.get(API_URl + "UsersServlet?methodType=getUser&userId=" + storage.id).success(function(data){
		$scope.myData = data.data;
	});
	$scope.mySet = function(myData){
		$http.get(API_URl + 'UsersServlet?methodType=updateUser&userId='+storage.id+'&nickname='+encodeURI(myData.nickname)+'&phoneNumber='+ myData.phone_number +'&name='+encodeURI(myData.name)+'&bio='+encodeURI(myData.bio)).success(function(data){
			if(data.status.code == 0){
				var successPopup = $ionicPopup.show({
					content: '修改成功!'
				});
				$timeout(function() {
					successPopup.close();
				}, 1500);
				storage.name = myData.name;
				storage.nickname = myData.nickname;
				storage.phoneNumber = myData.phone_number;
				storage.bio = myData.bio;
			}
		});

	};
})
//意见反馈
.controller('FeedbackCtrl',function($scope, $http, $timeout, $ionicPopup){
		//console.log(angular.element('#dom').scope());
//	console.log(document.getElementById('dom').innerHTML());
		//var dom = document.getElementById('dom');

		//angular.element('#dom');
		//angular.element(dom).html('ni mei')

		$scope.feedback = function(msg){
			console.log(msg.con);
			if(msg.con.replace(/\s/g,'')==''){
				var emptyPopup = $ionicPopup.show({
					content: '请输入!'
				});
				$timeout(function() {
					emptyPopup.close();
				}, 1500);
			}else{
				$http.get(API_URl + 'OpinionServlet?methodType=report&userId=' + storage.id + '&content=' + msg.con).success(function(data){
					if(data.status.code == 0){
						var successPopup = $ionicPopup.show({
							content: '提交成功!'
						});
						$timeout(function() {
							successPopup.close();
						}, 1500);
					}else{
						var errorPopup = $ionicPopup.show({
							content: data.status.msg
						});
						$timeout(function() {
							errorPopup.close();
						}, 1500);
					}
				});
			}


		}
})
//修改密码
.controller('ChangePasswordCtrl',function($scope, $state, $http, $timeout, $ionicPopup){
		$scope.changePassword = function(password){
			console.log( password.oldPassword );
			if( password.oldPassword == "" || password.newPassword == "" || password.repeatNewPassword == "" ){
				var emptyPopup = $ionicPopup.show({
					title:'提示',
					content: '请输入正确的密码!'
				});
				$timeout(function() {
					emptyPopup.close();
				}, 1500);

			}else if( password.newPassword !== password.repeatNewPassword ){
				var differencePopup = $ionicPopup.show({
					title:'提示',
					content: '两次输入的密码不一致!'
				});
				$timeout(function() {
					differencePopup.close();
				}, 1500);

			}else{
				$http.get(API_URl + 'UsersServlet?methodType=updatePassword&userId=' + storage.id + '&oldPassword=' + password.oldPassword + '&newPassword=' + password.newPassword).success(function(data){
					if(data.status.code == 0){
						var successPopup = $ionicPopup.show({
							title:'提示',
							content: '密码修改成功,请重新登录!'
						});
						$timeout(function() {
							successPopup.close();
							storage.clear();
							$state.go('signin')
						}, 1500);
					}else{
						var errorPopup = $ionicPopup.show({
							title:'提示',
							content:data.status.msg
						});
						$timeout(function() {
							errorPopup.close();
						}, 1500);

					}

				})
			}
		}
})

	//物业首页
	.controller('PropertyCtrl', function($scope){
		$scope.rightButtons = [{
			type: 'button-clear',
			content: 'Edit',
			tap: function(e) {
				alert(11)
			}
		}];

	})
	//居委会首页
	.controller('CommitteeCtrl', function($scope){

	})
	//物业、居委会简介
	.controller('PropertyIntroCtrl', function($scope, $http, $state){
		/*$scope.rightButtons = [{
			type: 'button-clear',
			content: 'Edit',
			tap: function(e) {
				alert(11)
			}
		}];
		console.log($scope.rightButtons);*/
		$scope.property_edit = function(){
			$state.go('app.property_intro_edit');
		};
		$http.get(API_URl + "IntroServlet?methodType=getById&introId="+storage.introId).success(function(data){
			$scope.property_intro = data.data;
			if(data.data.introType == 1){
				$scope.headTitle = '居委会'
			}else if(data.data.introType == 2){
				$scope.headTitle = '物业'
			}
		});
	})
	//物业、居委会简介编辑
	.controller('PropertyIntroEditCtrl', function($scope, $http, $state, $timeout, $ionicPopup){
		$http.get(API_URl + "IntroServlet?methodType=getById&introId="+storage.introId).success(function(data){
			$scope.property_intro = data.data;
			if(data.data.introType == 1){
				$scope.headTitle = '居委会'
			}else if(data.data.introType == 2){
				$scope.headTitle = '物业'
			}
		});
		$scope.property_complete = function(property_intro){
			$http.get(API_URl + "IntroServlet?methodType=update&introId="+ storage.introId +'&title=' + encodeURI(property_intro.title) + '&address=' + property_intro.address + '&phoneNumber=' + property_intro.phoneNumber + '&content=' + property_intro.content).success(function(data){
				if(data.status.code == 0){
					var successPopup = $ionicPopup.show({
						content: '修改成功!'
					});
					$timeout(function() {
						successPopup.close();
						history.back();
					}, 1500);

				}else{
					var errowPopup = $ionicPopup.show({
						content: '修改失败!'
					});
					$timeout(function() {
						errowPopup.close();
					}, 1500);
				}
			});
		};

	})
	//物业、居委会公告
	.controller('PropertyNoticeCtrl', function($scope, $http, $state){
		$http.get(API_URl + "NoticeServlet?methodType=getAll&introId="+storage.introId).success(function(data){
			if(data.status.code == 0) {
				var jsonData = [];
				for (var i = 0; i < data.data.length; i++) {
					var objData = data.data[i];
					objData.createTime = new Date(objData.createTime).format('MM-dd hh:mm');
					jsonData.push(objData);
				}
				$scope.property_notices = jsonData;
			}
		});
		$scope.notice_add = function(){
			$state.go('app.property_notice_post');
		}
	})
//物业、居委会发布公告
	.controller('PropertyNoticePostCtrl', function($scope, $http, $timeout, $ionicPopup){
		$scope.titleLen = {numLen:0};
		$scope.change = function(titleLen){
			titleLen.numLen = titleLen.title.length;
		};
		$scope.property_complete = function(titleLen){
			if(!titleLen.title||!titleLen.content){
				var emptyPopup = $ionicPopup.show({
					content: '请填写完整!'
				});
				$timeout(function() {
					emptyPopup.close();
				}, 1500);
			}else{
				$http.get(API_URl + 'NoticeServlet?methodType=add&introId='+storage.introId+'&title='+encodeURI(titleLen.title)+'&content='+encodeURI(titleLen.content)).success(function(data){
					 if(data.status.code == 0){
						 var successPopup = $ionicPopup.show({
							content: '添加成功!'
						 });
						 $timeout(function() {
							successPopup.close();
							 history.back();
						 }, 1500);
					 }
				});
			}

		}
	})
	//物业、居委会公告详情
	.controller('PropertyNoticeViewCtrl', function($scope, $state, $http, $stateParams, $timeout, $ionicPopup){

		$http.get(API_URl + "NoticeServlet?methodType=getById&noticeId="+$stateParams.id).success(function(data){
			$scope.property_notice_view = data.data;
		});
		$scope.notice_edit = function(){
			$state.go('app.property_notice_edit',{id:$stateParams.id});
		};
		//删除
		$scope.noticeDelete = function(){
			$http.get(API_URl + "NoticeServlet?methodType=delete&noticeId="+$stateParams.id).success(function(data){
				if(data.status.code == 0) {
					var successPopup = $ionicPopup.show({
						title:'提示',
						content: '删除成功!'
					});
					$timeout(function() {
						successPopup.close();
						$state.go('app.property_notice');
					}, 1000);
				}else{
					var failPopup = $ionicPopup.show({
						title:'提示',
						content: '删除失败，请稍后再试!'
					});
					$timeout(function() {
						failPopup.close();
						$state.go('app.property_notice');
					}, 1000);
				}
			})
		}
	})
	//物业、居委会编辑公告
	.controller('PropertyNoticeEditCtrl', function($scope, $http, $stateParams, $timeout, $ionicPopup){
		$http.get(API_URl + "NoticeServlet?methodType=getById&noticeId="+$stateParams.id).success(function(data){
			$scope.titleLen = data.data;
		});
		$scope.property_complete = function(titleLen){
			if(!titleLen.title||!titleLen.content){
				var emptyPopup = $ionicPopup.show({
					content: '请填写完整!'
				});
				$timeout(function() {
					emptyPopup.close();
				}, 1500);
			}else{
				$http.get(API_URl + 'NoticeServlet?methodType=update&noticeId='+$stateParams.id+'&title='+encodeURI(titleLen.title)+'&content='+encodeURI(titleLen.content)).success(function(data){
					if(data.status.code == 0){
						var successPopup = $ionicPopup.show({
							content: '编辑成功!'
						});
						$timeout(function() {
							successPopup.close();
							history.back();
						}, 1500);
					}
				});
			}

		}
	})
	//物业、居委会资讯
	.controller('PropertyNewsCtrl', function($scope, $state, $http){
		$http.get(API_URl + "InformationServlet?methodType=getAll&introId="+storage.introId).success(function(data){
			if(data.status.code == 0) {
				var jsonData = [];
				for (var i = 0; i < data.data.length; i++) {
					var objData = data.data[i];
					objData.createTime = new Date(objData.createTime).format('MM-dd hh:mm');
					jsonData.push(objData);
				}
				$scope.property_news = jsonData;
			}
		});
		$scope.news_add = function(){
			$state.go('app.property_news_add');
		};
	})

	//物业、居委会资讯详情
	.controller('PropertyNewViewCtrl', function($scope, $state, $http, $stateParams, $timeout, $ionicPopup){
		$scope.news_add = function(){
			$state.go('app.property_news_edit',{id:$stateParams.id});
		};
		$http.get(API_URl + "InformationServlet?methodType=getById&informationId="+$stateParams.id).success(function(data){
			$scope.property_new_view = data.data;
		});
		//删除
		$scope.newDelete = function(){
			$http.get(API_URl + "InformationServlet?methodType=delete&informationId="+$stateParams.id).success(function(data){
				if(data.status.code == 0) {
					var successPopup = $ionicPopup.show({
						title:'提示',
						content: '删除成功!'
					});
					$timeout(function() {
						successPopup.close();
						history.back();
					}, 1000);
				}else{
					var failPopup = $ionicPopup.show({
						title:'提示',
						content: '删除失败，请稍后再试!'
					});
					$timeout(function() {
						failPopup.close();
						$state.go('app.property_new');
					}, 1000);
				}
			})
		}
	})
//物业、居委会资讯添加
	.controller('PropertyNewAddCtrl', function($scope, $http, $timeout, $ionicPopup){
		$scope.titleLen = {numLen:0};
		$scope.change = function(titleLen){
			titleLen.numLen = titleLen.title.length;
		};
		$scope.add_complete = function(titleLen){
			if(!titleLen.title){
				var emptyPopup = $ionicPopup.show({
					content: '请填写完整!'
				});
				$timeout(function() {
					emptyPopup.close();
				}, 1500);
			}else{
				$http.get(API_URl + 'InformationServlet?methodType=add&introId='+storage.introId+'&content='+encodeURI(titleLen.title)).success(function(data){
					if(data.status.code == 0){
						var successPopup = $ionicPopup.show({
							content: '添加成功!'
						});
						$timeout(function() {
							successPopup.close();
							history.back();
						}, 1500);
					}
				});
			}

		}
	})
//物业、居委会资讯编辑
	.controller('PropertyNewEditCtrl', function($scope, $http, $stateParams, $timeout, $ionicPopup){
		$http.get(API_URl + "InformationServlet?methodType=getById&informationId="+$stateParams.id).success(function(data){
			$scope.titleLen = data.data;
		});
		$scope.add_complete = function(titleLen){
			if(!titleLen.content){
				var emptyPopup = $ionicPopup.show({
					content: '请填写完整!'
				});
				$timeout(function() {
					emptyPopup.close();
				}, 1500);
			}else{
				$http.get(API_URl + 'InformationServlet?methodType=update&informationId='+$stateParams.id+'&content='+encodeURI(titleLen.content)).success(function(data){
					if(data.status.code == 0){
						var successPopup = $ionicPopup.show({
							content: '添加成功!'
						});
						$timeout(function() {
							successPopup.close();
							history.back();
						}, 1500);
					}
				});
			}

		}
	})

;
